package com.ruoyi.gp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.SurveyVo;
import com.ruoyi.gp.domain.bo.SurveyBo;
import com.ruoyi.gp.service.ISurveyService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问卷
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/survey")
public class SurveyController extends BaseController {

    private final ISurveyService iSurveyService;

    /**
     * 查询问卷列表
     */
    @SaCheckPermission("system:survey:list")
    @GetMapping("/list")
    public TableDataInfo<SurveyVo> list(SurveyBo bo, PageQuery pageQuery) {
        return iSurveyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问卷列表
     */
    @SaCheckPermission("system:survey:export")
    @Log(title = "问卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SurveyBo bo, HttpServletResponse response) {
        List<SurveyVo> list = iSurveyService.queryList(bo);
        ExcelUtil.exportExcel(list, "问卷", SurveyVo.class, response);
    }

    /**
     * 获取问卷详细信息
     *
     * @param surveyId 主键
     */
    @SaCheckPermission("system:survey:query")
    @GetMapping("/{surveyId}")
    public R<SurveyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long surveyId) {
        return R.ok(iSurveyService.queryById(surveyId));
    }

    /**
     * 新增问卷
     */
    @SaCheckPermission("system:survey:add")
    @Log(title = "问卷", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SurveyBo bo) {
        return toAjax(iSurveyService.insertByBo(bo));
    }

    /**
     * 修改问卷
     */
    @SaCheckPermission("system:survey:edit")
    @Log(title = "问卷", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SurveyBo bo) {
        return toAjax(iSurveyService.updateByBo(bo));
    }

    /**
     * 删除问卷
     *
     * @param surveyIds 主键串
     */
    @SaCheckPermission("system:survey:remove")
    @Log(title = "问卷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{surveyIds}")
    public R<Void> remove(@NotEmpty(message = "问卷id不能为空")
                          @PathVariable Long[] surveyIds) {
        return toAjax(iSurveyService.deleteWithValidByIds(Arrays.asList(surveyIds), true));
    }


}
